package com.example.city_home_app.Activity;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;

import com.example.city_home_app.Bean.Down_Msg;
import com.example.city_home_app.Bean.Down_Universe;
import com.example.city_home_app.Model.ApiModel;
import com.example.city_home_app.R;
import com.example.city_home_app.RecyclerViewAdapter.Adapter_Msg;
import com.example.city_home_app.RecyclerViewAdapter.Adapter_MsgCenter;
import com.example.city_home_app.RetrofitListener;
import com.example.city_home_app.UI.UIUtils;
import com.zhihu.matisse.Matisse;
import com.zhihu.matisse.MimeType;
import com.zhihu.matisse.engine.impl.PicassoEngine;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MessageActivity extends BaseActivity {
    @BindView(R.id.toolbar_message)
    Toolbar toolbarMessage;
    @BindView(R.id.rec_message)
    RecyclerView recMessage;
    @BindView(R.id.et_message_content)
    EditText etMessageContent;
    @BindView(R.id.img_message_pic)
    ImageView imgMessagePic;
    @BindView(R.id.img_message_send)
    ImageView imgMessageSend;

    private int senduser;
    private List<Uri> list;
    private Adapter_Msg adapter_msg;
    private ApiModel model = new ApiModel();

    private RetrofitListener<List<Down_Msg>> listRetrofitListener = new RetrofitListener<List<Down_Msg>>() {
        @Override
        public void onResponse(List<Down_Msg> listener) {
            adapter_msg.setList(listener);
            adapter_msg.notifyDataSetChanged();
            recMessage.scrollToPosition(listener.size()-1);
        }

        @Override
        public void onFail(String msg) {

        }
    };

    private RetrofitListener<Down_Universe> sendlistener = new RetrofitListener<Down_Universe>() {
        @Override
        public void onResponse(Down_Universe listener) {
            if(listener.getMessage().equals("success")){
                UIUtils.showToast(MessageActivity.this,"发送成功",UIUtils.LENGTH_SHORT);
                model.getmessage(BaseActivity.getUserinfo().getUserid(),senduser,listRetrofitListener);
                etMessageContent.setText("");
            }
        }

        @Override
        public void onFail(String msg) {

        }
    };

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_message;
    }

    @Override
    protected void initViews() {
        super.initViews();
        ButterKnife.bind(this);
        showToast();

        recMessage = (RecyclerView)findViewById(R.id.rec_message);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recMessage.setLayoutManager(layoutManager);
        adapter_msg = new Adapter_Msg(this);
        recMessage.setAdapter(adapter_msg);
        recMessage.setItemAnimator(new DefaultItemAnimator());
    }

    @Override
    protected void initData() {
        super.initData();
        Intent intent = getIntent();
        senduser = intent.getIntExtra("sendid",0);
        toolbarMessage.setTitle(intent.getStringExtra("sendname"));
        model.getmessage(BaseActivity.getUserinfo().getUserid(),senduser,listRetrofitListener);
    }


    @OnClick({R.id.img_message_pic, R.id.img_message_send})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_message_pic:
                Matisse.from(this)
                        .choose(MimeType.of(MimeType.JPEG, MimeType.PNG, MimeType.GIF))
                        .countable(true)
                        .maxSelectable(1)
                        .restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
                        .thumbnailScale(0.85f)
                        .theme(R.style.Matisse_Zhihu)
                        .imageEngine(new PicassoEngine())
                        .capture(false)
                        .forResult(100);
                break;
            case R.id.img_message_send:
                if(!etMessageContent.getText().equals("")){
                    model.sendnormalmessage(BaseActivity.getUserinfo().getUserid(),senduser,1,etMessageContent.getText().toString(),sendlistener);
                }
                break;
        }
    }

    private void showToast() {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

        toolbarMessage = (Toolbar) findViewById(R.id.toolbar_message);
        setSupportActionBar(toolbarMessage);
        toolbarMessage.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (data != null) {
                if (requestCode == 100 && resultCode == RESULT_OK) {
                    list = Matisse.obtainResult(data);
                    String path = UIUtils.getFilePathFromUri(this, list.get(0));
                    model.sendimagemessage(BaseActivity.getUserinfo().getUserid(),senduser,2,path,sendlistener);
                }
            }
        }
    }
}
